package org.example.controller;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 自定义菜单
 * @author shenguangyang
 */
@RestController
public class CustomMenuController {
    @Resource
    private WxMpService wxMpService;

    /**
     * 创建菜单 <br/>
     * 我们来尝试创建2个按钮，第2个按钮包含3个子按钮，只要用WxJava封装好的菜单和按钮对象，非常方便：
     */
    @GetMapping("createMenu")
    public String createMenu() throws WxErrorException {
        // 创建菜单对象
        WxMenu menu = new WxMenu();
        // 创建按钮1
        WxMenuButton button1 = new WxMenuButton();
        button1.setType(WxConsts.MenuButtonType.CLICK);
        button1.setName("今日歌曲");
        button1.setKey("V1001_TODAY_MUSIC");
        // 创建按钮2
        WxMenuButton button2 = new WxMenuButton();
        button2.setName("菜单");
        // 创建按钮2的子按钮1
        WxMenuButton button21 = new WxMenuButton();
        button21.setType(WxConsts.MenuButtonType.VIEW);
        button21.setName("搜索");
        button21.setUrl("https://www.baidu.com/");
        // 创建按钮2的子按钮2
        WxMenuButton button22 = new WxMenuButton();
        button22.setType(WxConsts.MenuButtonType.VIEW);
        button22.setName("视频");
        button22.setUrl("https://v.qq.com/");
        // 创建按钮2的子按钮3
        WxMenuButton button23 = new WxMenuButton();
        button23.setType(WxConsts.MenuButtonType.CLICK);
        button23.setName("赞一下我们");
        button23.setKey("V1001_GOOD");
        // 将子按钮添加到按钮2
        button2.getSubButtons().add(button21);
        button2.getSubButtons().add(button22);
        button2.getSubButtons().add(button23);
        // 将按钮1和你按钮2添加到菜单
        menu.getButtons().add(button1);
        menu.getButtons().add(button2);
        // 创建按钮
        return wxMpService.getMenuService().menuCreate(menu);
    }
}
